package Servelts;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import daoFactory.DaoFactory;
import daoImpls.GoodDAOImpl;
import daoImpls.OrderDAOImpl;
import daoImpls.UserDAOImpl;
import vo.Category;
import vo.Good;
import vo.Order;
import vo.User;
@WebServlet("/listOrder")
public class OrderListServlet extends HttpServlet {

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String status = request.getParameter("status");
		String url = "error.jsp";
		//查询所有
		if(status.equals("selectall")){
			//获取当前页数，首次进入时start为0，点击超链接时获取start页数
			int start=request.getParameter("start")==null?0:Integer.parseInt(request.getParameter("start"));
			//通过page实现类获得总页数，这里的3为每页数目
			int pageSize = 5;
	        int totalPage = DaoFactory.getPageFactory().getOrderTotal(pageSize); 
	        int prePage = start-1;
	        if(prePage<0) prePage = 0;
	        int nextPage = start+1;
	        if(nextPage+1>totalPage) nextPage = totalPage-1;
	       
			request.setAttribute("totalPage", totalPage);
			request.setAttribute("prePage", prePage); 
			request.setAttribute("nextPage", nextPage);
			request.setAttribute("star", start+1);
			
			List<Order> orderlist = DaoFactory.getOrderFactory().list(start*pageSize,pageSize);
			request.getSession().setAttribute("orderlist", orderlist);
			request.getSession().setAttribute("size", orderlist.size());
			int totalsize = DaoFactory.getOrderFactory().list().size();
			request.setAttribute("totalsize", totalsize);
			url="listOrder.jsp";
		}
		//模糊查询
		if(status.equals("selectlike")){
			List<User> user = DaoFactory.getUserFactory().list(request.getParameter("key"));
			String ids = "";
			for (int i=0;i<user.size();i++) {
				if(i==user.size()-1){
					ids += user.get(i).getUid();
					break;
				}
				ids += user.get(i).getUid();
				ids += ",";
			}
			List<Order> orderlist = DaoFactory.getOrderFactory().listlike(ids);
			request.setAttribute("orderlist", orderlist);
			url="listOrder.jsp";
		}
		
		//范围查询
		if(status.equals("slectquery")){
			int begin = Integer.parseInt(request.getParameter("begin"));
			int end = Integer.parseInt(request.getParameter("end"));
			
			List<Order> orderlist = DaoFactory.getOrderFactory().list(begin,end);
			request.setAttribute("orderlist", orderlist);
			url="listOrder.jsp";
		}
		//删除
		if(status.equals("delect")){
			String oid = request.getParameter("oid");
			DaoFactory.getOrderFactory().delete(oid);
			url="listOrder?status=selectall";
		}
		//范围删除
		if(status.equals("delectquery")){
			String oids = request.getParameter("oids");
			DaoFactory.getOrderFactory().deleteQuery(oids);
			url="listOrder?status=selectall";
		}
		//添加
		if(status.equals("add")){
			String uid = request.getParameter("uid");
			String gid = request.getParameter("gid");
			int num = Integer.parseInt(request.getParameter("num"));
			int stat = Integer.parseInt(request.getParameter("stat"));
			Order order = new Order();
			order.setUser(DaoFactory.getUserFactory().get(uid));
			order.setGood(DaoFactory.getGoodFactory().get(gid));
			order.setoNum(num);
			order.setStatus(stat);
			DaoFactory.getOrderFactory().add(order);
			url="listOrder?status=selectall";
		}
		//跳转添加
		if(status.equals("addjsp")){
			List<Good> goodlist = DaoFactory.getGoodFactory().list();
			List<User> userlist = DaoFactory.getUserFactory().list();
			request.setAttribute("goodlist", goodlist);
			request.setAttribute("userlist", userlist);
			url = "Orderadd.jsp";
		}
		//更新
		if(status.equals("update")){
			Order order = DaoFactory.getOrderFactory().get(request.getParameter("oid"));
			int num = Integer.parseInt(request.getParameter("num"));
			int stat = Integer.parseInt(request.getParameter("stat"));
			User User = DaoFactory.getUserFactory().get(request.getParameter("uid"));
			Good good = DaoFactory.getGoodFactory().get(request.getParameter("gid"));
			order.setUser(User);
			order.setGood(good);
			order.setoNum(num);
			order.setStatus(stat);
			DaoFactory.getOrderFactory().update(order);
			url="listOrder?status=selectall";
		}
		//商品详情
		if(status.equals("orderinfo")){
			List<Good> goodlist = DaoFactory.getGoodFactory().list();
			List<User> userlist = DaoFactory.getUserFactory().list();
			Order order = DaoFactory.getOrderFactory().get(request.getParameter("oid"));
			for (int i = 0; i < goodlist.size(); i++) {
				Good good = goodlist.get(i);
				if(good.getgName().equals(order.getGood().getgName()))
					goodlist.remove(i);
			}
			for (int i = 0; i < userlist.size(); i++) {
				User user = userlist.get(i);
				if(user.getUname().equals(order.getUser().getUname()))
					userlist.remove(i);
			}
			request.setAttribute("goodlist", goodlist);
			request.setAttribute("userlist", userlist);
			request.setAttribute("order", order);
			
			url = "OrderInfo.jsp";
			
		}
		request.getRequestDispatcher(url).forward(request, response);
		
	}
}
